package com.dhcc.wb.qiye;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.dhcc.bussiness.page.PageBean;
import com.dhcc.common.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class QiyeAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ActionSupport.class);
	
	
	private QiyeDao dao = new QiyeDao();
	
	private Integer currentPage;
	private Integer currentCount = 10;
	private PageBean<QiyeModel> pageBean = new PageBean<QiyeModel>();
	private QiyeService qiyeService = new QiyeService();
	
	private String _id;
	private String result;
	private QiyeModel qiye;
	
	private String qiyeid;//
    private String qiyename;//
    private Integer px;//排序
    private String beizhu;
    private String parentid;
    
	
	
	public String qiye() throws Exception {
		// TODO Auto-generated method stub
		
		HttpServletRequest request = ServletActionContext.getRequest();
		int currentPage = Integer.parseInt(request.getParameter("currentPage")==null?"1":request.getParameter("currentPage"));//规定是第一页
		String name = request.getParameter("name");
		pageBean = qiyeService.findQiyeServiceByPage(currentPage,currentCount,name);
		ActionContext.getContext().getSession().put("pageBean", pageBean);
		return SUCCESS;
	}
	
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		int i = dao.delete(_id);
		if(i>0){
			result = "success";
		}
		return SUCCESS;
	}

	public String edit() throws Exception {
		// TODO Auto-generated method stub
		
		qiye = dao.queryqiye_result(_id);
		
		ActionContext.getContext().getSession().put("qiyeAll", dao.getAll());
		ActionContext.getContext().getSession().put("qiye", qiye);
		return SUCCESS;
	}
	
	public String save() throws Exception {
	    if(!StringUtil.isNullOrEmpty(qiyeid)){
	    	dao.updateQiye(qiyeid, qiyename, px, beizhu, parentid);
	    }else{
	    	dao.addQiye(UUID.randomUUID().toString(), qiyename, px, beizhu, parentid);
	    }
		return SUCCESS;
	}
	
	
	public String findById() {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String _id2 = request.getParameter("qiyeid");
		QiyeModel qiyse = dao.queryqiye_result(_id2);
		JSONObject jsons = new JSONObject();
		jsons.put("result",qiyse.getQiyename());
		result = jsons.toString();
		return SUCCESS;
	}


	public Integer getCurrentPage() {
		return currentPage;
	}



	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}



	public Integer getCurrentCount() {
		return currentCount;
	}



	public void setCurrentCount(Integer currentCount) {
		this.currentCount = currentCount;
	}

	public String get_id() {
		return _id;
	}

	public void set_id(String _id) {
		this._id = _id;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public QiyeModel getQiye() {
		return qiye;
	}

	public void setQiye(QiyeModel qiye) {
		this.qiye = qiye;
	}

	public String getQiyeid() {
		return qiyeid;
	}

	public void setQiyeid(String qiyeid) {
		this.qiyeid = qiyeid;
	}

	public String getQiyename() {
		return qiyename;
	}

	public void setQiyename(String qiyename) {
		this.qiyename = qiyename;
	}

	public Integer getPx() {
		return px;
	}

	public void setPx(Integer px) {
		this.px = px;
	}

	public String getBeizhu() {
		return beizhu;
	}

	public void setBeizhu(String beizhu) {
		this.beizhu = beizhu;
	}

	public String getParentid() {
		return parentid;
	}

	public void setParentid(String parentid) {
		this.parentid = parentid;
	}



	
}
